package com.zb.service.impl;

import com.google.common.collect.Lists;
import com.zb.dto.DeviceDTO;
import com.zb.dto.QuotaInfo;
import com.zb.es.EsRepository;
import com.zb.service.DeviceService;
import com.zb.service.TbQuotaService;
import com.zb.vo.DeviceQuotaVO;
import com.zb.vo.Pager;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeviceServiceImpl implements DeviceService {

    @Autowired
    private EsRepository esRepository;
    @Autowired
    private TbQuotaService tbQuotaService;

    @Override
    public Pager<DeviceQuotaVO> queryDeviceQuota(Long page, Long pageSize, String deviceId, String tag, Integer status) {
        //es中查询设备
        Pager<DeviceDTO> pager = esRepository.searchDevice(page, pageSize, deviceId, tag, status);
        //根据这边查询指标
        List<DeviceQuotaVO> deviceQuotaVOList = Lists.newArrayList();

        pager.getItems().forEach(deviceDTO -> {
            DeviceQuotaVO deviceQuotaVO = new DeviceQuotaVO();
            BeanUtils.copyProperties(deviceDTO, deviceQuotaVO);
            //查询最后一次的指标数据
            List<QuotaInfo> lastQuotaList = tbQuotaService.getLastQuotaList(deviceDTO.getDeviceId());
            deviceQuotaVO.setQuotaList(lastQuotaList);
            deviceQuotaVOList.add(deviceQuotaVO);
        });
        //封装返回结果数据
        Pager pagerResult = new Pager(pager.getCounts(), pageSize);
        pagerResult.setItems(deviceQuotaVOList);
        return pagerResult;
    }

    @Override
    public boolean updateOnline(String deviceId, Boolean online) {
        if (deviceId.startsWith("webclient") || deviceId.startsWith("monitor")) {
            return false;
        }
        DeviceDTO deviceDTO = esRepository.searchDeviceById(deviceId);
        if (deviceDTO == null) return false;
        deviceDTO.setOnline(online);
        return esRepository.updateOnline(deviceId, online);
    }

    @Override
    public boolean saveDeviceInfo(DeviceDTO deviceDTO) {
        DeviceDTO device = esRepository.searchDeviceById(deviceDTO.getDeviceId());
        if (device != null && !device.getStatus()) return false;
        if (device == null) {
            esRepository.addDevice(deviceDTO);
        } else {
            esRepository.updateDeviceAlarm(deviceDTO);
        }
        return true;
    }

    @Override
    public boolean setStatus(String deviceId, Boolean status) {
        return esRepository.updateStatus(deviceId, status);
    }

    @Override
    public boolean updateDeviceTag(String deviceId, String tag) {
        DeviceDTO deviceDTO = esRepository.searchDeviceById(deviceId);
        if (deviceDTO == null) return false;
        esRepository.updateDeviceTag(deviceId, tag);
        return true;
    }

    @Override
    public Pager<DeviceDTO> searchDevice(Long page, Long pageSize, String deviceId, String tag, Integer status) {
        return esRepository.searchDevice(page, pageSize, deviceId, tag, status);
    }
}
